package dichotomyMethod;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author kunkun
 * @className LeetCode_35
 * @Description 搜索插入位置
 * @date 2025/3/18 11:21
 */
public class LeetCode_35 {

    public int searchInsert(int[] nums, int target) {
        //问题转换为二分查找寻找小于目标数的最右数
        //1. 二分查找目标数
        int left =0 ,right = nums.length-1;
        while(left<right){
            int mid = (left+right)/2;
            if(nums[mid]<target){
                left = mid+1;
            }else if (nums[mid]>target){
                right = mid - 1;
            }else{
                return mid;
            }
        }
        if(nums[left]==target){
            return left;
        }else if (nums[left]>target){
            return left;
        }else{
            return left+1;
        }



    }


}
